package com.superhelper.receiver.service;

import java.util.Properties;

import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.SendResult;
import com.superhelper.common.Consts;
import com.superhelper.common.domain.baidu.BdPushEntity;
import com.superhelper.common.domain.dada.DaDaCallback;
import com.superhelper.common.domain.ele.ElePushEntity;
import com.superhelper.common.domain.mt.MtOrderPushEntity;
import com.superhelper.common.utils.JsonHelper;

//@Service
public final class ProducerService2 {
    private static final Logger log = LoggerFactory.getLogger(ProducerService2.class);

    private volatile Producer eleProducer = null;
    private volatile Producer mtProducer = null;
    private volatile Producer bdProducer = null;
    private volatile Producer dadaProducer = null;

    private Object eleMonitor = new Object();
    private Object mtMonitor = new Object();
    private Object bdMonitor = new Object();

    @Value("${aliyun.access.key}")
    private String aliyunAccessKey;
    @Value("${aliyun.access.secret}")
    private String aliyunSecretKey;

    @Value("${mt.order.producer.id}")
    private String mtProducerId;
    @Value("${mt.order.topic}")
    private String mtOrderTopic;

    @Value("${ele.order.producer.id}")
    private String eleProducerId;
    @Value("${ele.order.topic}")
    private String eleOrderTopic;

    @Value("${bd.order.producer.id}")
    private String bdProducerId;
    @Value("${bd.order.topic}")
    private String bdOrderTopic;

    @Value("${dada.topic}")
    private String dadaTopic;

    @Value("${dada.producer.id}")
    private String dadaProducerId;

    @Value("${online}")
    private boolean online;

    public Producer getEleOrderProducer() {
        if (eleProducer == null) {
            synchronized (eleMonitor) {
                if (eleProducer == null) {
                    Properties properties = new Properties();
                    properties.put(PropertyKeyConst.AccessKey, aliyunAccessKey);
                    properties.put(PropertyKeyConst.SecretKey, aliyunSecretKey);
                    properties.put(PropertyKeyConst.ProducerId, eleProducerId);
                    eleProducer = ONSFactory.createProducer(properties);
                    eleProducer.start();
                }
            }
        }
        return eleProducer;
    }

    public Producer getMtOrderProducer() {
        if (mtProducer == null) {
            synchronized (mtMonitor) {
                if (mtProducer == null) {
                    Properties properties = new Properties();
                    properties.put(PropertyKeyConst.AccessKey, aliyunAccessKey);
                    properties.put(PropertyKeyConst.SecretKey, aliyunSecretKey);
                    properties.put(PropertyKeyConst.ProducerId, mtProducerId);
                    mtProducer = ONSFactory.createProducer(properties);
                    mtProducer.start();
                }
            }
        }
        return mtProducer;
    }

    public Producer getBdOrderProducer() {
        if (bdProducer == null) {
            synchronized (bdMonitor) {
                if (mtProducer == null) {
                    Properties properties = new Properties();
                    properties.put(PropertyKeyConst.AccessKey, aliyunAccessKey);
                    properties.put(PropertyKeyConst.SecretKey, aliyunSecretKey);
                    properties.put(PropertyKeyConst.ProducerId, bdProducerId);
                    bdProducer = ONSFactory.createProducer(properties);
                    bdProducer.start();
                }
            }
        }
        return bdProducer;
    }

    public Producer getDadaProducer() {
        if (dadaProducer == null) {
            synchronized (eleMonitor) {
                if (dadaProducer == null) {
                    Properties properties = new Properties();
                    properties.put(PropertyKeyConst.AccessKey, aliyunAccessKey);
                    properties.put(PropertyKeyConst.SecretKey, aliyunSecretKey);
                    properties.put(PropertyKeyConst.ProducerId, dadaProducerId);
                    dadaProducer = ONSFactory.createProducer(properties);
                    dadaProducer.start();
                }
            }
        }
        return dadaProducer;
    }

    public boolean sendEleOrder(ElePushEntity entity) {
        String tag = eleOrderTopic + online;
        Message msg = new Message(eleOrderTopic, tag, JsonHelper.toJson(entity).getBytes(Consts.UTF8_CHARSET));
        msg.setKey("ele-" + entity.getRequestId());
        SendResult sr = getEleOrderProducer().send(msg);
        String msgId = sr.getMessageId();
        log.info("[sendEleOrder] success, key:{},msgId:{}", msg.getKey(), msgId);
        return true;
    }

    public boolean sendMtOrder(Long orderId, String pushType, MtOrderPushEntity entity) {
        String tag = mtOrderTopic + online;
        entity.setPushType(pushType);
        Message msg = new Message(mtOrderTopic, tag, JsonHelper.toJson(entity).getBytes(Consts.UTF8_CHARSET));
        msg.setKey("mt-" + orderId);
        SendResult sr = getMtOrderProducer().send(msg);
        String msgId = sr.getMessageId();
        log.info("[sendMtOrder]  success, key:{},msgId:{}", msg.getKey(), msgId);
        return true;
    }

    public boolean sendBdOrder(BdPushEntity entity) {
        String tag = bdOrderTopic + online;
        Message msg = new Message(bdOrderTopic, tag, JsonHelper.toJson(entity).getBytes(Consts.UTF8_CHARSET));
        msg.setKey("bd-" + entity.getSign());
        SendResult sr = getBdOrderProducer().send(msg);
        String msgId = sr.getMessageId();
        log.info("[sendBdOrder]  success, key:{},msgId:{}", msg.getKey(), msgId);
        return true;
    }

    @PreDestroy
    public void destroy() {
        if (mtProducer != null) {
            mtProducer.shutdown();
        }
        if (eleProducer != null) {
            eleProducer.shutdown();
        }
        if (bdProducer != null) {
            bdProducer.shutdown();
        }
        if (dadaProducer != null) {
            dadaProducer.shutdown();
        }
    }

    public boolean sendDada(DaDaCallback cb) {
        String tag = dadaTopic + online;
        Message msg = new Message(dadaTopic, tag, JsonHelper.toJson(cb).getBytes(Consts.UTF8_CHARSET));
        // 就算有重复消息也再发一遍，所有key选择加上毫秒数
        msg.setKey("dada-" + cb.getOrder_id() + "-" + System.currentTimeMillis());
        SendResult sr = getDadaProducer().send(msg);
        String msgId = sr.getMessageId();
        log.info("[sendDada]  success, key:{},msgId:{}", msg.getKey(), msgId);
        return true;
    }
}
